<template>

  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
    <el-col :span="20">
      <span>{{ nodeDate.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex">
        <!-- 两个内容 -->
        <el-col :span="12">{{ nodeDate.manager }}</el-col>
        <el-col :span="12">
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>

</template>

<script>

import { reqDelDepartment } from '@/api/departments'

export default {
  props: {
    /* 节点展示内容 */
    nodeDate: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false

    }

  },
  methods: {
    async handleCommand(type) {
      if (type === 'add') {
        // nodeDate在新增中,作为父亲
        this.$emit('add-depts', this.nodeDate)
      }
      if (type === 'edit') {
        // nodeDate在编辑中,作为自己
        this.$emit('edit-depts', this.nodeDate)
      }

      if (type === 'del') {
        const res = await this.$confirm('确定要永久删除吗', '提示').catch(error => error)
        if (res === 'cancel') return
        await reqDelDepartment(this.nodeDate.id)
        // 成功提示
        this.$message.success('删除成功')
        this.$emit('del-depts')
      }
    }
  }

}
</script>

<style>

</style>
